package com.mvp.mvpdemo.fragment

import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import com.cxz.wanandroid.ext.setSingleClickListener
import com.mvp.mvpdemo.R
import com.mvp.mvpdemo.adapter.SpaceItemDecoration
import com.mvp.mvpdemo.adapter.WxAdapter
import com.mvp.mvpdemo.base.BaseMvpFragment
import com.mvp.mvpdemo.bean.BAnnerBean
import com.mvp.mvpdemo.bean.LoginBean
import com.mvp.mvpdemo.bean.WXBean
import com.mvp.mvpdemo.bean.WXData
import com.mvp.mvpdemo.mvp.contract.HomeContract
import com.mvp.mvpdemo.mvp.presenter.HomePresenter
import kotlinx.android.synthetic.main.fragment_home.*


/**
 * Created By Mahongyin
 * Date    2020/10/15 17:06
 *
 */
class HomeFragment : BaseMvpFragment<HomeContract.View, HomeContract.Presenter>(),
    HomeContract.View {
   private var datas= ArrayList<WXData>()
    /**
     * RecyclerView Divider
     */
    private val recyclerViewItemDecoration by lazy {
        SpaceItemDecoration(requireContext())
    }

    private val wxAdapter: WxAdapter by lazy {
        WxAdapter(datas)
    }

    override fun createPresenter(): HomePresenter {
        return HomePresenter(requireContext())
    }

    override fun getLayoutId(): Int = R.layout.fragment_home

    override fun initCreate() {

    }

    override fun initData() {
        button3.setSingleClickListener { mPresenter?.login("myname", "mahongyin") }
        button4.setSingleClickListener {  mPresenter?.getBanner() }
        recycleView.apply {
            layoutManager = LinearLayoutManager(context)
            itemAnimator = DefaultItemAnimator()
            addItemDecoration(recyclerViewItemDecoration)
            adapter = wxAdapter
        }

        mPresenter?.getWX()

    }

    override fun susscess(result: LoginBean) {
        if (result.errorCode==0){
        tv_home.text = "昵称：${result.data.nickname}"}
    }

    override fun error() {
        showLongToast("error")
    }

    override fun getBanner(bean: BAnnerBean) {
        if (bean.errorCode==0){
        tv_home.text = "title:${bean.data[1].title}"}
    }

    override fun getWX(bean: WXBean) {
        datas= bean.data as ArrayList<WXData>
        wxAdapter.datas=datas
        wxAdapter.setNewInstance(datas)
        wxAdapter.notifyDataSetChanged()
    }


}